package org.hbgl.geoentity.db.service;

import com.github.pagehelper.PageHelper;
import org.hbgl.geoentity.db.dao.GeDepartmentMapper;
import org.hbgl.geoentity.db.dao.GeUserMapper;
import org.hbgl.geoentity.db.domain.GeUser;
import org.hbgl.geoentity.db.domain.GeUser.Column;
import org.hbgl.geoentity.db.domain.GeUserExample;
import org.hbgl.geoentity.db.util.EnumFile;
import org.hbgl.geoentity.db.vo.NodeVo;
import org.hbgl.geoentity.db.vo.UnitVo;
import org.hbgl.geoentity.db.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class GeUserService {
    @Resource
    private GeUserMapper userMapper;
    @Resource
    private GeDepartmentMapper departmentMapper;

    public List<UserVo> selectUserList(Integer unitId, Integer roleId, String keywords, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return userMapper.selectUserList(unitId, roleId, keywords);
    }

    // public List<UserVo> selectUserList(Integer roleId, String keywords, Integer page, Integer limit) {
    //     List<Integer> departmentIdList = departmentMapper.selectAllChildId(unitId, keywords);
    //     departmentIdList.add(departmentId);
    //
    //     PageHelper.startPage(page, limit);
    //     return userMapper.selectUserList(unitId, departmentIdList, roleId, keywords);
    // }

    public UserVo selectUserById(Integer unitId, Integer id) {
        return userMapper.selectUserById(unitId, id);
    }

    public UserVo selectUnitById(Integer unitId, Integer id) {
        return userMapper.selectUnitById(unitId, id);
    }

    public GeUser findById(Integer unitId, Integer id) {
        GeUserExample example = new GeUserExample();
        example.or().andUnitIdEqualTo(unitId).andIdEqualTo(id).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return userMapper.selectOneByExample(example);
    }

    public List<GeUser> findByUsername(String username) {
        GeUserExample example = new GeUserExample();
        example.or().andUsernameEqualTo(username).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return userMapper.selectByExample(example);
    }

    public List<GeUser> findByUsernameCheck(String username, boolean flag) {
        GeUserExample example = new GeUserExample();
        if(flag) {
            example.or().andUsernameEqualTo(username).andUnitIdEqualTo(1).andStateNotEqualTo(EnumFile.CommonState.Delete);
        }else {
            example.or().andUsernameEqualTo(username).andUnitIdNotEqualTo(1).andStateNotEqualTo(EnumFile.CommonState.Delete);
        }
        return userMapper.selectByExample(example);
    }

    public void add(GeUser user) {
        user.setState(EnumFile.CommonState.Normal);
        user.setAddTime(LocalDateTime.now());
        user.setEditTime(LocalDateTime.now());
        userMapper.insertSelective(user);
    }

    public int updateByIdSelective(GeUser user) {
        user.setEditTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public int updateByUnitIdSelective(GeUser user) {
        user.setEditTime(LocalDateTime.now());
        return userMapper.updateByUnitIdSelective(user);
    }

    public GeUser findPasswordById(Integer id) {
        return userMapper.selectByPrimaryKeySelective(id, Column.id, Column.password);
    }

    public List<GeUser> all(Integer unitId) {
        GeUserExample example = new GeUserExample();
        example.or().andUnitIdEqualTo(unitId).andStateEqualTo(EnumFile.CommonState.Normal);
        return userMapper.selectByExample(example);
    }

    /**
     * 更新账号信息（更新所有字段，需要判断是否需要审核）
     *
     * @param user 账号信息
     */
    @Transactional
    public int updateById(GeUser user) {
        user.setEditTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public NodeVo selectUserAuthByUnitId(Integer unitId) {
        return userMapper.selectUserAuthByUnitId(unitId);
    }

}
